#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "func.h"

void insere_medicamento(){

}
void eliminar_medicamento(){

}

void actualizar_stock(int argc, char*argv[]){
       
        FILE *f,*f2;
	f=fopen("fic.txt","r+");
	f2=fopen("tab.txt","r+");
	int nml=qualel(argv[1]);
	char Nome[20];
	int quant,i,e;
	long nlmed,nlmed2,linha;


	for(i=0;i<=nml;i++) fscanf(f2,"%lu",&nlmed);

	fscanf(f2,"%lu",&nlmed2);
	linha=nlmed*21;
	fseek(f,linha, SEEK_SET);
	printf("%lu %lu\n",nlmed,nlmed2);
	fscanf(f,"%s%d",Nome,&quant);

	e=eigual(Nome,argv[1]);
	while(e==0&&nlmed<nlmed2)
		{fscanf(f,"%s%d",Nome,&quant);
		e=eigual(Nome,argv[1]);
		nlmed++;
		}
	fclose(f);
	fclose(f2);
	if(e==0){
	printf("nao encontrado");
	exit(1);}
	else insere(Nome,quant,argv[2],nlmed);
}

void ler_medicamento(){

}

void top_vendas(){

}

insere(char Nome[],int quanta, char quant[], long linha)
{
	char *a[1];
	FILE *f;
	printf("linha:%lu\n",linha);
	quanta=quanta-atoi(quant);
	int i, p, c=tam(Nome)+tamn(quanta);
	linha=linha*21;
	f=fopen("fic.txt","r+");
	fseek(f,linha, SEEK_SET);
	i=20-c;
	char u[i];
	for(p=0;p<i;p++) u[p]=' ';
	u[p]='\0';
	fprintf(f,"%s%s%d",Nome,u,quanta);
	fclose(f);

}
int qualel(char s[])
{   int r,a=0,b=0;
    char ca='A',ba='A';
	while(s[0]!=ca) {
			a++;
		   	ca=ca+1;}
	while(s[1]!=ba)
		 {b++;
	   	  ba=ba+1;
		  }
		r=26*a+b;
		return r;
		}

int tam(char a[])
    {int a1=strlen(a);
	  return a1;
		    }
int tamn(int t)
{int a=1,b=0;
while(t>=a) {
	a=a*10;
	b++;
	}
	 return b;
	}


int eigual(char a[], char b[])
{
int i=0, a1=strlen(a), b1=strlen(b);
while(i!=a1&&i!=b1)
{if(a[i]<b[i]) return 0;
else if(a[i]>b[i])return 0;
else if(a[i]==b[i]) i++; }
if(a1==b1) return 1;
else return 0;
}
